

#include "BlockQueue.hpp"
#include "Task.hpp"
#include <pthread.h>
#include <ctime>
#include <unistd.h>


void* Consumer(void* args)
{
    BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*>(args);
    while (true)
    {
        // 获取数据 
        task_t t;
        bq->Pop(&t);
        // 处理数据 
        // t.Excute();
        t();
        // std::cout << "Consumer -> " << t.result() << std::endl;
    }
    
}


void* Productor(void* args)
{
    srand(time(nullptr) ^ getpid());
    BlockQueue<task_t>* bq = static_cast<BlockQueue<task_t>*>(args);
    while (true)
    {
        // sleep(2);
        // 构建任务数据 
        // int x = rand() % 10 + 1;
        // usleep(x * 1000);
        // int y = rand() % 10 + 1;
        // task_t t(x, y);
        
        // 生产数据 
        bq->Equeue(&Download);
        std::cout << "Productor -> " << "Download()" << std::endl;

        sleep(1);
    }
}
 

int main()
{
    BlockQueue<task_t>* bq = new BlockQueue<task_t>();

    pthread_t tid_c1, tid_c2;
    pthread_t tid_p1, tid_p2, tid_p3;
    pthread_create(&tid_c1, nullptr, Consumer, bq);
    pthread_create(&tid_c2, nullptr, Consumer, bq);
    pthread_create(&tid_p1, nullptr, Productor, bq);
    pthread_create(&tid_p2, nullptr, Productor, bq);
    pthread_create(&tid_p3, nullptr, Productor, bq);

    pthread_join(tid_c1, nullptr);
    pthread_join(tid_c2, nullptr);
    pthread_join(tid_p1, nullptr);
    pthread_join(tid_p2, nullptr);
    pthread_join(tid_p3, nullptr);
    return 0;
}

